﻿@{
    Layout = "~/Views/Shared/_Index.cshtml";
}
@using Raffles.OA.Data
@using Raffles.OA.Utility
<style>
    .table-hover > tbody > tr:hover,
    .bootstrap-table .fixed-table-container .table tbody tr.selected td {
        background-color: #FFDAB9;
        cursor: pointer;
    }

    .font {
        font-style: italic;
        font-size: 13px;
    }
    .fontnumber {
        font-style: italic;
        font-size: 15px;
    }
</style>
<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box box-info">
                <div class="box-body">
                    <div class="table-responsive">
                        <div class="btn-group-sm" id="toolbar" role="group">
                            <div class="col-sm-3 pull-right">
                                <div class="input-group input-group-sm">
                                    <input type="text" id="keyWord" col="KeyWord" class="form-control" placeholder="请输入关键字查询" />
                                    <div class="input-group-btn">
                                        <button type="button" class="btn btn-primary" onclick="searchGrid()"><i class="fa fa-search"></i></button>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <table id="gridTable" class="table table-hover text-nowrap">
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>
<script src="~/Content/plugin/bootstrap.table/1.12.0/bootstrap-table-merge-cells.js"></script>
<script type="text/javascript">
    var currentRowId = undefined;
    $(function () {
        initGrid();
        resetBootstrapTableView('gridTable', 69);
    });

    function initGrid() {
        $('#gridTable').frameTable({
            url: '@Url.Action("GetPayApplyPageListJson", "Project")',
            striped: false,
            columns: [
                {
                    field: 'Name', title: '项目名称', halign: "center", align: "center"
                },
                {
                    field: 'CountrySegmentCN', title: '行程分段', halign: "center", align: "center",
                    formatter: function (value, row, index) {
                        return '<span class="label label-primary">' + value + '</span>';
                    }
                },
                {
                    field: 'DepartureDate', title: '行程日期', halign: "center", align: "center",
                    formatter: function (value, row, index) {
                        return "<span class='text-red text-bold font'>" + moment(row.DepartureDate).format('YYYY.MM.DD') + " - " + moment(row.ReturnDate).format('YYYY.MM.DD') + "</span>";
                    }
                },
                {
                    field: '', title: '销售报价(RMB)', halign: "center", align: "center",
                    formatter: function (value, row, index) {
                        return "<span class='text-bold text-green fontnumber underline'>" + frame.formatCurrency(row.SaleRMBAmountAfterTax, "CNY") + "</span>";
                    }
                },
                {
                    field: '', title: '项目成本(RMB)', halign: "center", align: "center",
                    formatter: function (value, row, index) {
                        return "<span class='text-bold text-green fontnumber underline'>" + frame.formatCurrency(row.CostRMBAmountAfterTax, "CNY") + "</span>";
                    }
                },
                {
                    field: '', title: '销售利润(RMB)', halign: "center", align: "center",
                    formatter: function (value, row, index) {
                        return "<span class='text-bold text-green fontnumber underline'>" + frame.formatCurrency(row.SaleRMBAmountAfterTax - row.CostRMBAmountAfterTax, "CNY") + "</span>";
                    }
                },
                {
                    field: '', title: '毛利润率(%)', halign: "center", align: "center",
                    formatter: function (value, row, index) {
                        var moneyMakeRate = 0;
                        if (row.SaleRMBAmountAfterTax > 0) {
                            var moneyMakeRate = (row.SaleRMBAmountAfterTax - row.CostRMBAmountAfterTax) / row.SaleRMBAmountAfterTax * 100;
                        }
                        return "<span class='text-bold text-green fontnumber underline'>" + (moneyMakeRate.toFixed(2)) + "%</span>" + "</span>";
                    }
                },
                {
                    field: 'PayStatus', title: '申请状态', halign: "center", align: "center",
                    formatter: function (value, row, index) {
                        switch (value) {
                            case @((int)ProjectCostPayEnum.PayAudit):
                                return '<span class="label label-warning">@ProjectCostPayEnum.PayAudit.GetDescription()</span> ';
                            case @((int)ProjectCostPayEnum.PayAuditPass):
                                return '<span class="label label-success">@ProjectCostPayEnum.PayAuditPass.GetDescription()</span> ';
                        }
                    }
                },
                {
                    field: 'SaleManagerName', title: '销售经理', halign: "center", align: "center"
                },
                {
                    title: '操作',
                    halign: "center",
                    align: 'center',
                    formatter: function (value, row, index) { 
                        var actions = []; 
                        if (row.PayStatus ==@((int)ProjectCostPayEnum.PayAudit)) {
                            actions.push('<a href="javascript:void(0);" authorize="yes" id="btnApplyPass" class="btn btn-success btn-xs" onclick="auditApply(\'' + row.Id + '\',\'pass\')"><i class="fa fa-edit"></i> 同意</a> ');
                            actions.push('<a href="javascript:void(0);" authorize="yes" id="btnApplyRefuse" class="btn btn-danger btn-xs" onclick="auditApply(\'' + row.Id + '\',\'refuse\')"><i class="fa fa-trash-o"></i> 驳回</a>');
                        }
                        
                        else if (row.PayStatus ==@((int)ProjectCostPayEnum.PayAuditPass)) {
                            actions.push('<a href="javascript:void(0);" authorize="yes" id="btnCompletePayApply" class="btn btn-warning btn-xs" onclick="completePayApply(\'' + row.Id + '\')"><i class="fa fa-telegram"></i> 支付完成</a>');
                        }
                        return actions.join('');
                    }
                }
            ],
            queryParams: function (params) {
                var pagination = $('#gridTable').frameTable('getPagination', params);
                var queryString = $('#toolbar').getWebControls(pagination);
                return queryString;
            },
        });
    }

    function searchGrid() {
        $('#gridTable').frameTable('search');
    }

    //付款审核
    function auditApply(id, type) {
        frame.confirm((type == "pass" ? "确定<span class='text-green text-bold'>同意</span>当前项目成本支付申请吗？" :"确定<span class='text-red text-bold'>驳回</span>当前项目成本支付申请吗？"), function () {
            frame.ajax({
                url: '@Url.Content("/Finance/Project/AuditPayApply")' + '?projectId=' + id + "&type=" + type,
                type: "post",
                success: function (obj) {
                    if (obj.success) {
                        frame.msgSuccess(obj.message);
                        searchGrid();
                    }
                    else {
                        frame.msgError(obj.message);
                    }
                }
            });
        });
    }

    //支付完成
    function completePayApply(id) {
        frame.confirm("确认已经完成当前分段的成本支付吗？", function () {
            frame.ajax({
                url: '@Url.Content("/Finance/Project/CompletePayApply")' + '?projectId=' + id,
                type: "post",
                success: function (obj) {
                    if (obj.success) {
                        frame.msgSuccess(obj.message);
                        searchGrid();
                    }
                    else {
                        frame.msgError(obj.message);
                    }
                }
            });
        });
    }
</script>
